package top.defaults.colorpicker;

import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.agp.utils.Point;
import ohos.app.Context;
import ohos.app.dispatcher.TaskDispatcher;
import top.defaults.colorpicker.util.NumCalcUtil;

import static top.defaults.colorpicker.Constants.SELECTOR_RADIUS_DP;

/**
 * ColorWheelSelector
 *
 * @author: jjy
 * @since 2021-04-16
 */
public class ColorWheelSelector extends Component implements Component.DrawTask {

    private Paint selectorPaint;
    private float selectorRadiusPx = SELECTOR_RADIUS_DP * 3;
    private Point currentPoint;
    private TaskDispatcher globalTaskDispatcher;
    private Runnable runnable;

    /**
     * 构造函数
     *
     * @param context context对象
     */
    public ColorWheelSelector(Context context) {
        this(context, null);
    }

    /**
     * 构造函数
     *
     * @param context context对象
     * @param attrs 属性值
     */
    public ColorWheelSelector(Context context, AttrSet attrs) {
        this(context, attrs, 0);
    }

    /**
     * 构造函数
     *
     * @param context context对象
     * @param attrs 属性值
     * @param defStyleAttr 样式名称
     */
    public ColorWheelSelector(Context context, AttrSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        selectorPaint = new Paint();
        selectorPaint.setAntiAlias(true);
        selectorPaint.setColor(Color.BLACK);
        selectorPaint.setStyle(Paint.Style.STROKE_STYLE);
        selectorPaint.setStrokeWidth(2);
    }

    /**
     * viewsize改变监听
     *
     * @param width width
     * @param height height
     */
    protected void onSizeChanged(int width, int height) {
        float centerX = 0;
        float centerY = 0;
        currentPoint = new Point(centerX, centerY);
    }

    @Override
    public void invalidate() {
        this.addDrawTask(this::onDraw);
    }

    public void setSelectorRadiusPx(float selectorRadiusPx) {
        this.selectorRadiusPx = selectorRadiusPx;
    }

    /**
     * 设置当前坐标
     *
     * @param currentPoint 当前坐标点
     */
    public void setCurrentPoint(Point currentPoint) {
        this.currentPoint = currentPoint;
        reInvalidate();
    }

    private void reInvalidate() {
        if (mContext == null) {
            return;
        }
        globalTaskDispatcher = mContext.getMainTaskDispatcher();
        runnable = new Runnable() {
            @Override
            public void run() {
                invalidate();
            }
        };
        globalTaskDispatcher.asyncDispatch(runnable);
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        if (currentPoint == null) {
            return;
        }
        canvas.drawLine(NumCalcUtil.subtract(currentPoint.getPointX(), selectorRadiusPx), currentPoint.getPointY(),
                NumCalcUtil.add(currentPoint.getPointX(), selectorRadiusPx), currentPoint.getPointY(), selectorPaint);
        canvas.drawLine(currentPoint.getPointX(), NumCalcUtil.subtract(currentPoint.getPointY(), selectorRadiusPx),
                currentPoint.getPointX(), NumCalcUtil.add(currentPoint.getPointY(), selectorRadiusPx), selectorPaint);
        canvas.drawCircle(currentPoint.getPointX(), currentPoint.getPointY(), NumCalcUtil.multiply(selectorRadiusPx, 0.66f), selectorPaint);
    }
}
